import { _decorator, Vec3 , Node} from 'cc';
import { SkillManager } from './SkillManager';
import { EBuff, EGroup, EntityType, ESkill } from '../Config/Enum';
import ObjectPoolManager from '../Core/ObjectPoolManager';
import { Collider2D } from 'cc';
import { PlayerManager } from '../PlayerManager';
import { BattleManager } from '../BattleManager';
import { buffConfig } from '../Config/SkillConfig';
import { EnemyManager } from '../EnemyManager';
import { WallManager } from '../WallManager';

const { ccclass } = _decorator;

// 符文1 狮威震野- 武学范围扩大100%，附加城墙血量10%的伤害
// 符文2 慑魂梵音- 给敌人附加攻击-30%和移速-20%
// 符文3 伏魔狮吼- 使其承受自身伤害的持续伤害持续5s

@ccclass('ShaolinSkill2')
export class ShaolinSkill2 extends SkillManager {
    isMasterBullet
    runeId = 0
    numOfBullets = 1; // 默认是3个
    level
    buffAp
    buffDuration
    buffTypeList: EBuff[]
    effectChance
    em
    // 使用私有字段而非布尔标志
    private _hasSplit = false;
    private _contactPosition: Vec3 = new Vec3(0, 0, 0);
    

    init(type: ESkill, pm: PlayerManager) {
        // // 如果是子子弹，直接super.init(type, pm); 直接释放
        // if (!this.isMasterBullet) {
        //     super.init(type, pm);
        //     return;
        // }

        // // 如果是主子弹，就拿numOfBullets，并判断runeId，走逻辑，再释放
        // if (this.isMasterBullet) {
        //     this._hasSplit = false;
        // }

        super.init(type, pm);




        switch (this.runeId) {
            case 1:
                this.node.setScale(4, 4, 1); // 增大范围

                break;
            case 2:
                break;  
            case 3:
                // if (!this._hasSplit) {
                //     this.splitIntoMultipleBullets(2); // 释放两次
                // }
                break;
            default:
                break;
        }


    }

protected onHitEnemy(self, other: Collider2D): void {
    this.em = other.node.getComponent(EnemyManager);


    switch (this.runeId) {
        case 1:
            console.log("shaolinskill2 this.runeId:",this.runeId)
            this.damage += WallManager.Instance.hp * 0.1;


            break;
        case 2:

            console.log("shaolinskill2 this.runeId:",this.runeId)
            console.log("shaolinskill2 this.em:",this.em)
            
            //if(Math.random() < this.effectChance){
            this.applyBuffToEnemy(this.em, 1,EBuff.Stun, 0, 2); // 晕眩敌人2秒
            this.applyBuffToEnemy(this.em, 1,EBuff.AttackDown, 0.2, 2); // 减少敌人攻击20% 2s
            //}
            break;  
            case 3:
                console.log("shaolinskill2 this.runeId:", this.runeId);
                // 使其承受自身伤害的持续伤害持续5s
                let damage = this.em.ap;
                this.schedule(() => {
                    if (this.em && this.em.isValid) {
                        this.em.hurt(damage);
                        console.log("ShaolinSkill2 rune3 damage:", damage);
                    }
                }, 1, 5, 1); // 添加 true 参数使用真实时间
                break;
        default:
            console.log("ShaolinSkill2 no rune", this.runeId);
            break;
    }

    // 调用父类方法处理伤害
    super.onHitEnemy(self, other);
    

}

}